﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.BLL;
using SIS.ENT;

public partial class school_timetable_details : System.Web.UI.Page
{
    private string CurrentTimeTableSessionName = "CurrentTimeTableSessionName";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Page.Request.Params["ttid"] != null && Page.Request.Params["clsrmid"] != null)
            {
                ShowTimeTable(Convert.ToInt32(Request.Params["ttid"].ToString()), Convert.ToInt32(Request.Params["clsrmid"].ToString()));
            }
            else if (Page.Request.Params["ttid"] != null)
            {
                ShowTimeTable(Convert.ToInt32(Request.Params["ttid"].ToString()), 0);
            }
            else
            {
                Response.Redirect("timetable.aspx");
            }
        }
    }

    private void ShowTimeTable(Int32 TimeTableKey, Int32 ClassRoomKey)
    {
        DataTable _dtTimeTable = new DataTable();
        _dtTimeTable = TimeTableManager.GetItemTable(TimeTableKey);

        if (_dtTimeTable != null && _dtTimeTable.Rows.Count > 0)
        {
            lblTimeTableKey.Text = TimeTableKey.ToString();
            lblSchoolKey.Text = _dtTimeTable.Rows[0]["SchoolKey"].ToString();
            lblLocationKey.Text = _dtTimeTable.Rows[0]["LocationKey"].ToString();
            lblSchoolMajorKey.Text = _dtTimeTable.Rows[0]["MajorKey"].ToString();
            lblStandardLevelKey.Text = _dtTimeTable.Rows[0]["StandardLevelKey"].ToString();

            lblSchoolName.Text = _dtTimeTable.Rows[0]["SchoolName"].ToString();
            lblPeriodDesc.Text = _dtTimeTable.Rows[0]["PeriodDesc"].ToString() + " (" + _dtTimeTable.Rows[0]["EducationPeriodCode"].ToString() + ")";
            lblPeriodDuration.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodStartDate"].ToString()).ToString("dd/MMM/yyyy") + " to " + Convert.ToDateTime(_dtTimeTable.Rows[0]["PeriodEndDate"].ToString()).ToString("dd/MMM/yyyy");
            lblMajor.Text = _dtTimeTable.Rows[0]["MajorDesc"].ToString();

            lblStandardLevel.Text = _dtTimeTable.Rows[0]["StandardLevelDesc"].ToString();

            lblLocation.Text = _dtTimeTable.Rows[0]["LocationName"].ToString();
            lblDayGroupDesc.Text = _dtTimeTable.Rows[0]["DayGroupDesc"].ToString();
            lblDayDecriptionList.Text = _dtTimeTable.Rows[0]["DayDescriptionList"].ToString();

            string[] strCurrentDayList = lblDayDecriptionList.Text.Split(',');
            for (int d = 0; d < strCurrentDayList.Length; d++)
            {
                if (strCurrentDayList[d].Trim().ToUpper() == "MONDAY")
                {
                    gvTimeTableDetails.Columns[1].Visible = true;
                }
                else if (strCurrentDayList[d].Trim().ToUpper() == "TUESDAY")
                {
                    gvTimeTableDetails.Columns[2].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "WEDNESDAY")
                {
                    gvTimeTableDetails.Columns[3].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "THURSDAY")
                {
                    gvTimeTableDetails.Columns[4].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "FRIDAY")
                {
                    gvTimeTableDetails.Columns[5].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SATURDAY")
                {
                    gvTimeTableDetails.Columns[6].Visible = true;
                }
                if (strCurrentDayList[d].Trim().ToUpper() == "SUNDAY")
                {
                    gvTimeTableDetails.Columns[7].Visible = true;
                }
            }


            lblStartTime.Text = _dtTimeTable.Rows[0]["StartTime"].ToString();
            lblSectionDuration.Text = _dtTimeTable.Rows[0]["SectionDuration"].ToString();
            lblNoOfSection.Text = _dtTimeTable.Rows[0]["NoOfSection"].ToString();
            lblBreakTimeCount.Text = _dtTimeTable.Rows[0]["BreakTimeNo"].ToString();

            if (lblBreakTimeCount.Text == "0")
            {
                lblBreakTimeDesc.Text = "No Break time";
            }
            else
            {
                lblBreakTimeDesc.Text = "Break Every " + lblBreakTimeCount.Text + " section(s)";
            }
            lblBreakTimeCount.Visible = false;

            TimeSpan _breakTime1Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime1Duration"];
            lblBreakTime1DurationHour.Text = _breakTime1Duration.Hours.ToString();
            lblBreakTime1DurationMin.Text = _breakTime1Duration.Minutes.ToString();

            TimeSpan _breakTime2Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime2Duration"];
            lblBreakTime2DurationHour.Text = _breakTime2Duration.Hours.ToString();
            lblBreakTime2DurationMin.Text = _breakTime2Duration.Minutes.ToString();

            TimeSpan _breakTime3Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime3Duration"];
            lblBreakTime3DurationHour.Text = _breakTime3Duration.Hours.ToString();
            lblBreakTime3DurationMin.Text = _breakTime3Duration.Minutes.ToString();

            TimeSpan _breakTime4Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime4Duration"];
            lblBreakTime4DurationHour.Text = _breakTime4Duration.Hours.ToString();
            lblBreakTime4DurationMin.Text = _breakTime4Duration.Minutes.ToString();

            TimeSpan _breakTime5Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime5Duration"];
            lblBreakTime5DurationHour.Text = _breakTime5Duration.Hours.ToString();
            lblBreakTime5DurationMin.Text = _breakTime5Duration.Minutes.ToString();

            TimeSpan _breakTime6Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime6Duration"];
            lblBreakTime6DurationHour.Text = _breakTime6Duration.Hours.ToString();
            lblBreakTime6DurationMin.Text = _breakTime6Duration.Minutes.ToString();

            TimeSpan _breakTime7Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime7Duration"];
            lblBreakTime7DurationHour.Text = _breakTime7Duration.Hours.ToString();
            lblBreakTime7DurationMin.Text = _breakTime7Duration.Minutes.ToString();

            TimeSpan _breakTime8Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime8Duration"];
            lblBreakTime8DurationHour.Text = _breakTime8Duration.Hours.ToString();
            lblBreakTime8DurationMin.Text = _breakTime8Duration.Minutes.ToString();

            TimeSpan _breakTime9Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime9Duration"];
            lblBreakTime9DurationHour.Text = _breakTime9Duration.Hours.ToString();
            lblBreakTime9DurationMin.Text = _breakTime9Duration.Minutes.ToString();

            TimeSpan _breakTime10Duration = (TimeSpan)_dtTimeTable.Rows[0]["BreakTime10Duration"];
            lblBreakTime10DurationHour.Text = _breakTime10Duration.Hours.ToString();
            lblBreakTime10DurationMin.Text = _breakTime10Duration.Minutes.ToString();

            lblModifiedDateTime.Text = Convert.ToDateTime(_dtTimeTable.Rows[0]["ModifiedDateTime"].ToString()).ToString("dd/MMM/yyyy hh:mm tt");

            GetDayList();
            GetTimeList();
            ddlClassRoom.DataBind();

            if (ddlClassRoom.Items.Count > 0)
            {
                if (ClassRoomKey > 0)
                {
                    ddlClassRoom.Text = ClassRoomKey.ToString();
                }
                else
                {
                    ddlClassRoom.SelectedIndex = 0;
                }
                ddlClassRoom_SelectedIndexChanged(null, null);
            }
        }
    }

    private DataTable GenerateTimeTableDetails(DataTable dtTimeTableDetail)
    {
        DataTable myTTDetails = new DataTable("tblTimeTableDetails");

        myTTDetails.Columns.Add("StartTimeAndDay");

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 0, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);


        string[] strDayList = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                myTTDetails.Columns.Add(strDayList[d].ToString().Trim());
            }
        }

        myTTDetails.Columns.Add("SectionNo");

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;

        bool _IsBreakTimeIgnore = false;

        for (int r = 0; r < _rowCount; r++)
        {
            DataRow _dr = myTTDetails.NewRow();
            _IsBreakTimeIgnore = false;
            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime1DurationMin.Text);
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                }
                
                if (_hour == 0 && _min == 0)
                {
                    _IsBreakTimeIgnore = true;
                }
                else
                {
                    tsTempBT = new TimeSpan(_hour, _min, 0);

                    _dr[0] = "Break Time (" + tsTempBT.Hours.ToString("00") + ":" + tsTempBT.Minutes.ToString("00") + ")";

                    tsNextTime += tsTempBT;
                }

                
                _currentBTNo += 1;
            }
            else
            {
                _currentSectionNo += 1;
                _startTime = tsNextTime.ToString();
                _dr[0] = tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
                tsNextTime += tsSectionDuration;
                _dr[0] += " to " + tsNextTime.Hours.ToString("00") + ":" + tsNextTime.Minutes.ToString("00");
            }

            for (int d = 0; d < strDayList.Length; d++)
            {
                if (_currentBTCount == _breakTime)
                {
                    if (!_IsBreakTimeIgnore)
                    {
                        _dr[d + 1] = "BreakTime";
                    }
                }
                else
                {
                    
                    if (dtTimeTableDetail != null && dtTimeTableDetail.Rows.Count > 0)
                    {
                        DataRow[] _selectRows = dtTimeTableDetail.Select("SectionNo = " + _currentSectionNo.ToString() + " AND DayName = '" + strDayList[d].ToString() + "'");
                        if (_selectRows != null && _selectRows.Length > 0)
                        {
                            _dr[d + 1] = _selectRows[0]["SubjectName"].ToString() + "<br/>(" + _selectRows[0]["FirstName"].ToString() + " " + _selectRows[0]["LastName"].ToString() + ")";
                        }
                        else
                        {
                            _dr[d + 1] = "Create New";
                        }
                    }
                    else
                    {
                        _dr[d + 1] = "Create New";
                    }
                }
            }

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
                r--;
            }
            else
            {
                _currentBTCount += 1;
            }

            _dr[strDayList.Length + 1] = _currentSectionNo.ToString();

            if (!_IsBreakTimeIgnore)
            {
                myTTDetails.Rows.Add(_dr);
            }
        }

        return myTTDetails;
    }

    private void GetTimeList()
    {
        ddlTimeDesc.Items.Clear();
        ddlStartTime.Items.Clear();
        ddlEndTime.Items.Clear();

        string _startTime = lblStartTime.Text;
        int _hour = Convert.ToInt32(_startTime.Substring(0, 2));
        int _min = Convert.ToInt32(_startTime.Substring(3, 2));
        TimeSpan tsStartTime = new TimeSpan(_hour, _min, 0);
        TimeSpan tsNextTime = tsStartTime;

        string _strSectionDuration = lblSectionDuration.Text;
        _hour = Convert.ToInt32(_strSectionDuration.Substring(0, 2));
        _min = Convert.ToInt32(_strSectionDuration.Substring(3, 2));
        TimeSpan tsSectionDuration = new TimeSpan(_hour, _min, 0);

        TimeSpan tsTempBT = new TimeSpan(0, 15, 0);

        int _breakTime = Convert.ToInt32(lblBreakTimeCount.Text);

        int _rowCount = Convert.ToInt32(lblNoOfSection.Text);

        int _currentBTCount = 0;
        int _currentBTNo = 1;
        int _currentSectionNo = 0;
        string _ItemDesc = string.Empty;
        bool _IsBreakTimeIgnore = false;
        for (int r = 0; r < _rowCount; r++)
        {
            tsStartTime = tsNextTime;
            _IsBreakTimeIgnore = false;
            if (_currentBTCount == _breakTime && _breakTime > 0)
            {
                if (_currentBTNo == 1)
                {
                    _hour = Convert.ToInt32(lblBreakTime1DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime1DurationMin.Text);
                }
                else if (_currentBTNo == 2)
                {
                    _hour = Convert.ToInt32(lblBreakTime2DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime2DurationMin.Text);
                }
                else if (_currentBTNo == 3)
                {
                    _hour = Convert.ToInt32(lblBreakTime3DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime3DurationMin.Text);
                }
                else if (_currentBTNo == 4)
                {
                    _hour = Convert.ToInt32(lblBreakTime4DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime4DurationMin.Text);
                }
                else if (_currentBTNo == 5)
                {
                    _hour = Convert.ToInt32(lblBreakTime5DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime5DurationMin.Text);
                }
                else if (_currentBTNo == 6)
                {
                    _hour = Convert.ToInt32(lblBreakTime6DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime6DurationMin.Text);
                }
                else if (_currentBTNo == 7)
                {
                    _hour = Convert.ToInt32(lblBreakTime7DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime7DurationMin.Text);
                }
                else if (_currentBTNo == 8)
                {
                    _hour = Convert.ToInt32(lblBreakTime8DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime8DurationMin.Text);
                }
                else if (_currentBTNo == 9)
                {
                    _hour = Convert.ToInt32(lblBreakTime9DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime9DurationMin.Text);
                }
                else if (_currentBTNo == 10)
                {
                    _hour = Convert.ToInt32(lblBreakTime10DurationHour.Text);
                    _min = Convert.ToInt32(lblBreakTime10DurationMin.Text);
                }

                if (_hour == 0 && _min == 0)
                {
                    _IsBreakTimeIgnore = true;
                }
                else
                {
                    tsTempBT = new TimeSpan(_hour, _min, 0);
                    _ItemDesc = "Break Time " + _currentBTNo.ToString() + " (" + tsTempBT.Hours.ToString("00") + " : " + tsTempBT.Minutes.ToString("00") + ")";
                    tsNextTime += tsTempBT;
                }

                _currentBTNo += 1;
            }
            else
            {
                _ItemDesc = tsNextTime.ToString();
                tsNextTime += tsSectionDuration;
                _ItemDesc += " to " + tsNextTime.ToString();
            }

            //ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
            //ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
            //ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));

            if (_currentBTCount == _breakTime)
            {
                _currentBTCount = 0;
                r--;
            }
            else
            {
                _currentSectionNo += 1;

                if (!_IsBreakTimeIgnore)
                {
                    ddlTimeDesc.Items.Add(new ListItem(_ItemDesc, _currentSectionNo.ToString()));
                    ddlStartTime.Items.Add(new ListItem(tsStartTime.ToString(), tsStartTime.ToString()));
                    ddlEndTime.Items.Add(new ListItem(tsNextTime.ToString(), tsNextTime.ToString()));
                }
                
                _currentBTCount += 1;
            }
        }
    }

    private void GetDayList()
    {
        ddlDayCode.Items.Clear();

        string[] strDayList = lblDayDecriptionList.Text.Split(',');
        if (strDayList != null && strDayList.Length > 0)
        {
            for (int d = 0; d < strDayList.Length; d++)
            {
                string _strDayName = strDayList[d].ToLower().Trim();

                if (_strDayName == "monday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "1"));
                }
                else if (_strDayName == "tuesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "2"));
                }
                else if (_strDayName == "wednesday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "3"));
                }
                else if (_strDayName == "thursday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "4"));
                }
                else if (_strDayName == "friday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "5"));
                }
                else if (_strDayName == "saturday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "6"));
                }
                else if (_strDayName == "sunday")
                {
                    ddlDayCode.Items.Add(new ListItem(_strDayName.ToUpper(), "7"));
                }
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        TimeTableDetail _ttd = new TimeTableDetail();
        DataTable _dtTTD = TimeTableDetailManager.GetItemByTeacherProfileKey(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlTeacherName.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToInt16(ddlTimeDesc.Text));
        if (_dtTTD != null && _dtTTD.Rows.Count > 0)
        {
            if (_dtTTD.Rows[0]["ClassRoomKey"].ToString() != ddlClassRoom.Text)
            {
                lblMessage.Text = "Selected teacher and subject is found on (" + _dtTTD.Rows[0]["ClassRoomCode"].ToString() + ") " + _dtTTD.Rows[0]["ClassRoomName"].ToString() + "'s schedule at same time.";
                lblMessage.ForeColor = System.Drawing.Color.Red;
                return;
            }
        }

        _ttd.TimeTableDetailKey = Convert.ToInt32(lblTimeTableDetailKey.Text);
        _ttd.TimeTableKey = Convert.ToInt32(lblTimeTableKey.Text);
        _ttd.DayCode = Convert.ToInt16(ddlDayCode.Text);
        _ttd.ClassRoomKey = Convert.ToInt32(ddlClassRoom.Text);
        _ttd.SubjectKey = Convert.ToInt32(ddlSubjectName.Text);
        _ttd.TeacherProfileKey = Convert.ToInt32(ddlTeacherName.Text);
        _ttd.SectionNo = Convert.ToInt16(ddlTimeDesc.Text);

        int _hour = Convert.ToInt32(ddlStartTime.Text.Substring(0, 2));
        int _min = Convert.ToInt32(ddlStartTime.Text.Substring(3, 2));
        _ttd.StartTime = new TimeSpan(_hour, _min, 0);

        _hour = Convert.ToInt32(ddlEndTime.Text.Substring(0, 2));
        _min = Convert.ToInt32(ddlEndTime.Text.Substring(3, 2));
        _ttd.EndTime = new TimeSpan(_hour, _min, 0);

        _ttd.UserID = 0;
        _ttd.Status = true;

        if (TimeTableDetailManager.Save(_ttd) > 0)
        {
            lblTimeTableDetailKey.Text = _ttd.TimeTableDetailKey.ToString();
            ddlClassRoom_SelectedIndexChanged(sender, e);
            lblMessage.Text = "Saved successfully.";
            lblMessage.ForeColor = System.Drawing.Color.Green;
        }
        else
        {
            lblMessage.Text = "Saving failed.";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        try
        {
            lblTimeTableDetailKey.Text = "-1";
            ddlTimeDesc.SelectedIndex = 0;
            ddlStartTime.SelectedIndex = 0;
            ddlEndTime.SelectedIndex = 0;
            ddlSubjectName.SelectedIndex = 0;
            ddlTeacherName.SelectedIndex = 0;
            lblMessage.Text = string.Empty;
            lblTopMessage.Text = string.Empty;
            
        }
        catch (Exception Ex)
        {

            lblTopMessage.Text = "Error found on btnNew_Click.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void ddlTimeDesc_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ddlStartTime.SelectedIndex = ddlTimeDesc.SelectedIndex;
            ddlEndTime.SelectedIndex = ddlTimeDesc.SelectedIndex;
            TimeTableDetail _ttd = new TimeTableDetail();
            _ttd = TimeTableDetailManager.GetItemBySectionNo(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlClassRoom.Text), Convert.ToInt16(ddlDayCode.Text), Convert.ToInt16(ddlTimeDesc.Text));
            if (_ttd != null && _ttd.TimeTableDetailKey > 0)
            {
                lblTopMessage.Text = "Found a schedule on selected day/time.";
                lblTopMessage.ForeColor = System.Drawing.Color.Green;
                lblTimeTableDetailKey.Text = _ttd.TimeTableDetailKey.ToString();
                ddlSubjectName.DataBind();
                if (ddlSubjectName.Items.Count > 0)
                {
                    ddlSubjectName.Text = _ttd.SubjectKey.ToString();
                    ddlTeacherName.DataBind();
                    if (ddlTeacherName.Items.Count > 0)
                    {
                        ddlTeacherName.Text = _ttd.TeacherProfileKey.ToString();
                    }
                }
            }
            else
            {
                lblTopMessage.Text = "Create a new schedule on selected day/time.";
                lblTopMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on ddlTimeDesc_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void ddlClassRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            btnNew_Click(sender, e);
            if (lblTimeTableKey.Text != "-1" && ddlClassRoom.Items.Count > 0)
            {
                DataTable _dtTimeTableByClassRom = TimeTableDetailManager.GetListTableByClassRoomKey(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(ddlClassRoom.Text));
                gvTimeTableDetails.DataSource = GenerateTimeTableDetails(_dtTimeTableByClassRom);
            }
            else
            {
                gvTimeTableDetails.DataSource = GenerateTimeTableDetails(null);
            }

            gvTimeTableDetails.DataBind();
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on ddlClassRoom_SelectedIndexChanged.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void ddlDayCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlTimeDesc.Items.Count > 0)
        {
            ddlTimeDesc.SelectedIndex = 0;
            ddlTimeDesc_SelectedIndexChanged(sender, e);
        }
    }

    protected void gvTimeTableDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink _hplMonday = (HyperLink)e.Row.FindControl("hplMonday");
                _hplMonday.Text = DataBinder.Eval(e.Row.DataItem, "Monday").ToString();

                LinkButton _btnMonday = (LinkButton)e.Row.FindControl("btnMonday");
                _btnMonday.CommandName = "UpdateSchedule";
                _btnMonday.CommandArgument = "1," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnMonday.ToolTip = "Monday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();
                if (_hplMonday.Text.Trim() != string.Empty)
                {
                    if (_hplMonday.Text == "BreakTime")
                    {
                        _btnMonday.Visible = false;
                        _hplMonday.Visible = true;
                    }
                    else
                    {
                        _btnMonday.Text = _hplMonday.Text;
                    }
                }

                HyperLink _hplTuesday = (HyperLink)e.Row.FindControl("hplTuesday");
                if (DataBinder.Eval(e.Row.DataItem, "Tuesday") != null)
                {
                    _hplTuesday.Text = DataBinder.Eval(e.Row.DataItem, "Tuesday").ToString();
                }

                LinkButton _btnTuesday = (LinkButton)e.Row.FindControl("btnTuesday");
                _btnTuesday.CommandName = "UpdateSchedule";
                _btnTuesday.CommandArgument = "2," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnTuesday.ToolTip = "Tuesday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();
                if (_hplTuesday.Text.Trim() != string.Empty)
                {
                    if (_hplTuesday.Text == "BreakTime")
                    {
                        _btnTuesday.Visible = false;
                        _hplTuesday.Visible = true;
                    }
                    else
                    {
                        _btnTuesday.Text = _hplTuesday.Text;
                    }
                }

                HyperLink _hplWednesday = (HyperLink)e.Row.FindControl("hplWednesday");
                _hplWednesday.Text = DataBinder.Eval(e.Row.DataItem, "Wednesday").ToString();

                LinkButton _btnWednesday = (LinkButton)e.Row.FindControl("btnWednesday");
                _btnWednesday.CommandName = "UpdateSchedule";
                _btnWednesday.CommandArgument = "3," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnWednesday.ToolTip = "Wednesday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplWednesday.Text.Trim() != string.Empty)
                {
                    if (_hplWednesday.Text == "BreakTime")
                    {
                        _btnWednesday.Visible = false;
                        _hplWednesday.Visible = true;
                    }
                    else
                    {
                        _btnWednesday.Text = _hplWednesday.Text;
                    }
                }

                HyperLink _hplThursday = (HyperLink)e.Row.FindControl("hplThursday");
                _hplThursday.Text = DataBinder.Eval(e.Row.DataItem, "Thursday").ToString();

                LinkButton _btnThursday = (LinkButton)e.Row.FindControl("btnThursday");
                _btnThursday.CommandName = "UpdateSchedule";
                _btnThursday.CommandArgument = "4," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnThursday.ToolTip = "Thursday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplThursday.Text.Trim() != string.Empty)
                {
                    if (_hplThursday.Text == "BreakTime")
                    {
                        _btnThursday.Visible = false;
                        _hplThursday.Visible = true;
                    }
                    else
                    {
                        _btnThursday.Text = _hplThursday.Text;
                    }
                }

                HyperLink _hplFriday = (HyperLink)e.Row.FindControl("hplFriday");
                _hplFriday.Text = DataBinder.Eval(e.Row.DataItem, "Friday").ToString();

                LinkButton _btnFriday = (LinkButton)e.Row.FindControl("btnFriday");
                _btnFriday.CommandName = "UpdateSchedule";
                _btnFriday.CommandArgument = "5," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnFriday.ToolTip = "Friday, " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplFriday.Text.Trim() != string.Empty)
                {
                    if (_hplFriday.Text == "BreakTime")
                    {
                        _btnFriday.Visible = false;
                        _hplFriday.Visible = true;
                    }
                    else
                    {
                        _btnFriday.Text = _hplFriday.Text;
                    }
                }

                HyperLink _hplSaturday = (HyperLink)e.Row.FindControl("hplSaturday");
                _hplSaturday.Text = DataBinder.Eval(e.Row.DataItem, "Saturday").ToString();

                LinkButton _btnSaturday = (LinkButton)e.Row.FindControl("btnSaturday");
                _btnSaturday.CommandName = "UpdateSchedule";
                _btnSaturday.CommandArgument = "6," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnSaturday.ToolTip = "Saturday " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplSaturday.Text.Trim() != string.Empty)
                {
                    if (_hplSaturday.Text == "BreakTime")
                    {
                        _btnSaturday.Visible = false;
                        _hplSaturday.Visible = true;
                    }
                    else
                    {
                        _btnSaturday.Text = _hplSaturday.Text;
                    }
                }

                HyperLink _hplSunday = (HyperLink)e.Row.FindControl("hplSunday");
                _hplSunday.Text = DataBinder.Eval(e.Row.DataItem, "Sunday").ToString();

                LinkButton _btnSunday = (LinkButton)e.Row.FindControl("btnSunday");
                _btnSunday.CommandName = "UpdateSchedule";
                _btnSunday.CommandArgument = "7," + DataBinder.Eval(e.Row.DataItem, "SectionNo").ToString();
                _btnSunday.ToolTip = "Sunday " + DataBinder.Eval(e.Row.DataItem, "StartTimeAndDay").ToString();

                if (_hplSunday.Text.Trim() != string.Empty)
                {
                    if (_hplSunday.Text == "BreakTime")
                    {
                        _btnSunday.Visible = false;
                        _hplSunday.Visible = true;
                    }
                    else
                    {
                        _btnSunday.Text = _hplSunday.Text;
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Error found on gvTimeTableDetails_RowDataBound.<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void gvTimeTableDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "UpdateSchedule")
            {
                btnNew_Click(sender, e);
                string _commandArgs = e.CommandArgument.ToString();
                string _dayCode = _commandArgs.Split(',')[0].ToString();
                string _sectionNo = _commandArgs.Split(',')[1].ToString();

                ddlDayCode.Text = _dayCode;
                ddlTimeDesc.Text = _sectionNo;
                ddlTimeDesc_SelectedIndexChanged(sender, null);

                LinkButton _btnLink = (LinkButton)e.CommandSource;

                for (int r = 0; r < gvTimeTableDetails.Rows.Count; r++)
                {
                    for (int c = 0; c < gvTimeTableDetails.Rows[r].Cells.Count; c++)
                    {
                        Control _lastLinkButton = gvTimeTableDetails.Rows[r].Cells[c].FindControl(lblLastSelectedLinkButtonID.Text);

                        if (_lastLinkButton != null)
                        {
                            LinkButton _lnkLast = (LinkButton)_lastLinkButton;
                            _lnkLast.ForeColor = System.Drawing.Color.Blue;
                            _lnkLast.Font.Bold = false;
                            break;
                        }
                    }
                }

                lblLastSelectedLinkButtonID.Text = _btnLink.ID.ToString();
                _btnLink.Font.Bold = true;
                _btnLink.ForeColor = System.Drawing.Color.Red;
            }


        }
        catch (Exception Ex)
        {
            lblTopMessage.Text = "Invalid section day/time. Not found on timetable.<br/>";
            lblTopMessage.Text += "Error found at gvTimeTableDetails_RowCommand<br/>" + Ex.Message;
            lblTopMessage.ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (Convert.ToInt32(lblTimeTableKey.Text) > 0 && Convert.ToInt32(lblTimeTableDetailKey.Text) > 0)
        {
            if (TimeTableDetailManager.Delete(Convert.ToInt32(lblTimeTableKey.Text), Convert.ToInt32(lblTimeTableDetailKey.Text)))
            {
                btnNew_Click(sender, e);
                lblMessage.Text = "Deleted successfully!";
                lblMessage.ForeColor = System.Drawing.Color.Green;
                ddlClassRoom_SelectedIndexChanged(sender, e);
            }
            else
            {
                lblMessage.Text = "Deleting failed!";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }

        }
        else
        {
            lblMessage.Text = "Please select a schedule to delete!";
            lblMessage.ForeColor = System.Drawing.Color.Red;
        }

    }

    protected void btnBackToTimeTableSetup_Click(object sender, EventArgs e)
    {
        Response.Redirect("timetable.aspx?ttid=" + lblTimeTableKey.Text);
    }

    protected void btnTakeAttendance_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/student/attendance.aspx?ttid=" + lblTimeTableKey.Text);
    }
}
